package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.role.BatchDeletePositionTypeRequest;
import com.mes.cloud.api.dto.request.role.BindPositionTypeRequest;
import com.mes.cloud.api.dto.request.role.PositionTypePageListRequest;
import com.mes.cloud.api.dto.response.role.RolePositionTypeDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysPositionTypeRoleDao;
import com.mes.cloud.entity.SysPositionTypeRole;
import com.mes.cloud.entity.vo.role.PositionTypeVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 角色岗位类型关联表
 *
 * @author wxm
 * @version 2019-08-03 14:47:34
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class SysPositionTypeRoleService {

    private SysPositionTypeRoleDao sysPositionTypeRoleDao;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public SysPositionTypeRoleService(SysPositionTypeRoleDao sysPositionTypeRoleDao, IPKeyGenerator iPKeyGenerator) {
        this.sysPositionTypeRoleDao = sysPositionTypeRoleDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param positionTypePageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<RolePositionTypeDto>> positionTypePageList(PositionTypePageListRequest positionTypePageListRequest) {
        Page<PositionTypeVo> page = PageHelper.startPage(positionTypePageListRequest.getCurrentPage(), positionTypePageListRequest.getPageSize());
        //查询条件
        List<PositionTypeVo> positionTypeVoList = sysPositionTypeRoleDao.positionTypePageList(positionTypePageListRequest);
        List<RolePositionTypeDto> rolePositionTypeDtoList = new ArrayList<>();
        for (PositionTypeVo positionTypeVo : positionTypeVoList) {
            RolePositionTypeDto rolePositionTypeDto = new RolePositionTypeDto();
            BeanUtils.copyProperties(positionTypeVo, rolePositionTypeDto);
            rolePositionTypeDtoList.add(rolePositionTypeDto);
        }
        CommPager<RolePositionTypeDto> pageResult = new CommPager<>(page, rolePositionTypeDtoList);
        JsonResult<CommPager<RolePositionTypeDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }


    /**
     * 角色增加岗位类型
     *
     * @param bindPositionTypeRequest
     * @return
     */
    public JsonResult bindPositionType(BindPositionTypeRequest bindPositionTypeRequest) {
        JsonResult jsonResult = new JsonResult();
        //获取角色已有岗位类型
        SysPositionTypeRole sysPositionTypeRole = new SysPositionTypeRole();
        sysPositionTypeRole.setRoleId(bindPositionTypeRequest.getRoleId());
        //未删除状态
        sysPositionTypeRole.setDelFlag(Constants.DEL_FLAG_NO);
        List<SysPositionTypeRole> existPositionTypeList = sysPositionTypeRoleDao.select(sysPositionTypeRole);

        //剔除员工已有岗位类型
        Set<String> idList = deleteExistPositionType(existPositionTypeList, bindPositionTypeRequest.getIds());

        //组装员工角色对象
        List<SysPositionTypeRole> sysPositionTypeRoleList = generatorSysPositionTypeRole(idList, bindPositionTypeRequest.getRoleId());
        if (CollectionUtils.isEmpty(sysPositionTypeRoleList)) {
            return jsonResult.setCode(HttpStatusEnum.REF_EXIST.getCode()).setMsg(HttpStatusEnum.REF_EXIST.getMessage());
        }
        //批量插入
        int count = sysPositionTypeRoleDao.batchInsert(sysPositionTypeRoleList);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 组织SysPositionTypeRole
     *
     * @param idList
     * @param roleId
     * @return
     */
    private List<SysPositionTypeRole> generatorSysPositionTypeRole(Set<String> idList, String roleId) {
        List<SysPositionTypeRole> sysPositionTypeRoleList = new ArrayList<SysPositionTypeRole>();
        if (CollectionUtils.isEmpty(idList)) {
            return sysPositionTypeRoleList;
        }
        SysPositionTypeRole sysPositionTypeRoleTmep = null;
        for (String id : idList) {
            sysPositionTypeRoleTmep = new SysPositionTypeRole();
            sysPositionTypeRoleTmep.setId(iPKeyGenerator.getGenerateKey());
            sysPositionTypeRoleTmep.setPositionType(id);
            sysPositionTypeRoleTmep.setRoleId(roleId);
            sysPositionTypeRoleTmep.setUpdateDateStr(DateUtil.updateDateStr());
            sysPositionTypeRoleTmep.setCreateBy(UserUtil.getCreateByName());
            sysPositionTypeRoleTmep.setUpdateBy(UserUtil.getUpdateByName());
            sysPositionTypeRoleTmep.setCreateByNo(UserUtil.getCreateByNo());
            sysPositionTypeRoleTmep.setUpdateByNo(UserUtil.getUpdateByNo());
            sysPositionTypeRoleList.add(sysPositionTypeRoleTmep);
        }
        return sysPositionTypeRoleList;
    }

    /**
     * 剔除角色现有的岗位类型
     *
     * @param existPositionTypeList 已有的岗位类型列表
     * @param ids                   需要添加的岗位类型列表
     * @return
     */
    private Set<String> deleteExistPositionType(List<SysPositionTypeRole> existPositionTypeList, Set<String> ids) {
        if (CollectionUtils.isEmpty(existPositionTypeList)) {
            return ids;
        }
        List<String> existIdList = new ArrayList<>();
        //员工已经拥有的岗位类型存放到existIdList
        for (SysPositionTypeRole sysPositionTypeRole : existPositionTypeList) {
            existIdList.add(sysPositionTypeRole.getPositionType());
        }
        return deleteExistId(existIdList, ids);
    }

    /**
     * existIdList  idList做比较,返回idList中不存在existIdList中的id
     *
     * @param existIdList
     * @param idList
     * @return
     */
    private Set<String> deleteExistId(List<String> existIdList, Set<String> idList) {
        Set<String> returnList = new HashSet<>();
        for (String id : idList) {
            //判断id是否存在existIdList列表中
            if (!existIdList.contains(id)) {
                returnList.add(id);
            }
        }
        return returnList;
    }

    /**
     * 批量删除
     *
     * @param batchDeletePositionTypeRequest
     * @return JsonResult
     */
    public JsonResult batchDelete(BatchDeletePositionTypeRequest batchDeletePositionTypeRequest) {
        JsonResult jsonResult = new JsonResult();
        int count = sysPositionTypeRoleDao.batchDelete(batchDeletePositionTypeRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

}
